﻿/* $RCSFile: Licenses.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/Licenses/Licenses.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:21  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:55  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;  
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Data.SqlClient;
using System.Web.Services.Protocols;
using System.IO;
using System.ComponentModel;
using System.Globalization;

using ADNCRM.ServerControls;
using AddonNice.Core;
using AddonNice.UI;
using AddonNice.Design;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Settings;

using Telerik.Web.UI;
using Telerik.Web.UI.Upload;

using Trace2    =   System.Diagnostics.Trace;
using Path2     =   AddonNice.Settings.Path;



namespace AddonNice.PageModules.Licenses
{
 	#region Local TraceSwitch 
	public class LicenseTrace : ADNTraceSwitch
	{
		static public ADNTraceSwitch	Sw	=	new ADNTraceSwitch("LicenseTrace","Special Trace Switch for Licenses");
	}
	#endregion Local TraceSwitch 

    public partial class Licenses : PortalModuleControl
    {
        #region General implementation

        public override bool AdminModule
        {
            get
            {
                return true;
            }
        }

        public override Guid GuidID
        {
            get
            {
                return new Guid("{D7DE6949-41FF-43ee-8B11-5190A492A4D9}");
            }
        }
        #endregion General implementation

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Licenses Page_Load BEG IsPostBack: {0}.",IsPostBack),ModuleTraceSwitch.Sw.Info);
        }

        void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Licenses RadGrid1_NeedDataSource BEG RebindReason: {0}.",e.RebindReason),ModuleTraceSwitch.Sw.Info);
            DataTable   tb                  =   new DataTable();
            tb.Columns.Add("Product",typeof(string));
            tb.Columns.Add("LicensePIN",typeof(string));
            try
            {
                // Check license for this package
                Company complic             =   Company.GetConfig();
                // Read Company installed by addoNice install
                LbCompany.Text              =   complic.Name;
                foreach (LicenseKey lkey in complic.Licenses)
                {
                    DataRow rw              =   tb.NewRow();
                    rw["Product"]           =   lkey.Product;
                    rw["LicensePIN"]        =   lkey.LicensePIN;
                    tb.Rows.Add(rw);
                    if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Licenses RadGrid1_NeedDataSource Addded Product: {0}, Pin: {1}.",lkey.Product,lkey.LicensePIN),ModuleTraceSwitch.Sw.Info);
                }
            }
            catch (Exception ex)
            {
                if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]Licenses RadGrid1_NeedDataSource ex: {0}.",ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
            RadGrid1.DataSource             =   tb;
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]Licenses RadGrid1_NeedDataSource END.",ModuleTraceSwitch.Sw.Info);
        }
        
        void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
        {
    		if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]Licenses RadGrid1_UpdateCommand BEG.",ModuleTraceSwitch.Sw.Info);
            try
            {
                GridEditableItem editedItem =   e.Item as GridEditableItem;
                Hashtable newValues         =   new Hashtable();
                editedItem.ExtractValues(newValues);
                object ob                   =   newValues["LicensePIN"];
    		    if ( ModuleTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Licenses RadGrid1_UpdateCommand LicensePIN: {0}.",ob.GetType()),ModuleTraceSwitch.Sw.Info);
                string LicensePIN           =   (string)ob;
                string Product              =   (string)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Product"];
                Company.CreateSection(LbCompany.Text,Product,LicensePIN);
                // must do a real postback to refresh data.....
                ADNPage.Redirect(Request.RawUrl,true);
			}
			catch(Exception ex)
			{
		        if ( ModuleTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]Licenses RadGrid1_UpdateCommand ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
			}
        }

        #region Std Control override
 
        /// <summary>
        /// Triggered from Page.OnPreInit
        /// We must set the Add button before buttons are initialized by base method
        public override void Page_PreInit(object sender, EventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Licenses Page_PreInit BEG IsPosBack: {0}", IsPostBack),ModuleTraceSwitch.Sw.Info);
            base.Page_PreInit(sender, e);
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]Licenses Page_PreInit END.",ModuleTraceSwitch.Sw.Info);
        }

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
        

        private void InitializeComponent()
        {
            Load                            +=  new EventHandler(Page_Load);
            RadGrid1.NeedDataSource         +=  new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
            RadGrid1.UpdateCommand          +=  new GridCommandEventHandler(RadGrid1_UpdateCommand);
        }

      #endregion Std Control override
    }
}